using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace cs2335.lab5
{
    /// <summary>
    /// sdavis31 - the Menu Item class
    /// </summary>
    public class MenuItem : IComponent
    {

        protected Rectangle menuItemRectangle = new Rectangle(0, 0, 200, 20);
        protected StringFormat labelFormat = new StringFormat(StringFormatFlags.NoClip);
        protected SolidBrush menuItemBrush = new SolidBrush(Color.Ivory);
        protected Font font = new Font("SansSerif", 10);
        protected CCTForm owner;
        protected string text;
        protected bool hasFocus = false;
        protected bool hidden = false;
        protected string name = "Menu";

        protected bool firstOption = false; // sdavis31 - whether the Menu Item is the first
        protected bool lastOption = false; // sdavis31 - whether the Menu Item is the last
        protected int optionNumber; // sdavis31 - the number of the Menu Item in relation to the menu order
        protected Menu parentMenu; // sdavis31 - the Menu object that the Menu Item belongs to

        #region Properties
        public Point Location
        {
            get { return menuItemRectangle.Location; }
            set { menuItemRectangle.Location = value; Invalidate(); }
        }

        public Rectangle Bounds
        {
            get { return menuItemRectangle; }
            set { menuItemRectangle = value; Invalidate(); }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public bool Hidden
        {
            get { return hidden; }
        }

        /// <summary>
        /// sdavis31 - public access to whether the Menu Item is the first one in the Menu
        /// </summary>
        public bool FirstOption
        {
            get { return firstOption; }
            set { firstOption = value; }
        }

        /// <summary>
        /// sdavis31 - public access to whether the Menu Item is the last one in the Menu
        /// </summary>
        public bool LastOption
        {
            get { return lastOption; }
            set { lastOption = value; }
        }

        /// <summary>
        /// sdavis31 - public access to the number of the Menu Item
        /// </summary>
        public int OptionNumber
        {
            get { return optionNumber; }
            set { optionNumber = value; }
        }

        /// <summary>
        /// sdavis31 - public access for whether the Menu Item has focus
        /// </summary>
        public bool HasFocus
        {
            get { return hasFocus; }
            set { hasFocus = value; }
        }

        /// <summary>
        /// sdavis31 - public access for the parent Menu object
        /// </summary>
        public Menu ParentMenu
        {
            get { return parentMenu; }
            set { parentMenu = value; }
        }

        public System.Drawing.Font Font
        {
            get { return font; }
            set { font = value; Invalidate(); }
        }

        public string Text
        {
            get { return text; }
            set { text = value; Invalidate(); }
        }

        public CCTForm Owner
        {
            get { return owner; }
            set { owner = value; }
        }

        public Color BackColor
        {
            get { return menuItemBrush.Color; }
            set { menuItemBrush.Color = value; Invalidate(); }
        }
        #endregion

        #region Events
        public event EventHandler Click;
        public event EventHandler GotFocus;
        public event EventHandler KeyPress;
        public event EventHandler KeyDown;
        public event EventHandler LostFocus;
        public event EventHandler Paint;
        #endregion

        /// <summary>
        /// sdavis31 - instantiating a Menu Item
        /// </summary>
        /// <param name="caption">caption and name of Menu Item</param>
        /// <param name="first">bool for whether first item in menu</param>
        /// <param name="last">bool for whether last item in menu</param>
        /// <param name="order">number of Menu Item</param>
        /// <param name="x">x coordinate</param>
        /// <param name="y">y coordinate</param>
        public MenuItem(string caption, bool first, bool last, int order, int x, int y)
        {
            // sdavis31 - left alignment
            labelFormat.LineAlignment = StringAlignment.Near;
            labelFormat.Alignment = StringAlignment.Near;
            text = caption;
            name = caption;
            firstOption = first;
            lastOption = last;
            optionNumber = order;
            Location = new Point(x, y);
            // sdavis31 - start with Menu Items hidden since Menu is not initially invoked
            hidden = true;
        }

        public MenuItem()
        {
            hidden = true;
        }

        public MenuItem(string s)
        {
            text = s;
            hidden = true;
        }

        public void Invalidate()
        {
            if (owner != null)
            {
                owner.Invalidate();
            }
        }

        public void Hide()
        {
            hidden = true;
            Invalidate();
        }

        public void Show()
        {
            hidden = false;
            Invalidate();
        }

        public void OnPaint(PaintEventArgs e)
        {
            if (!hidden)
            {
                Graphics g = e.Graphics;
                if (!hasFocus)
                {
                    g.FillRectangle(menuItemBrush, menuItemRectangle);
                    //Draw3DBorder(g);
                    g.DrawString(text, font, Brushes.Black, (RectangleF)menuItemRectangle, labelFormat);
                }
                if (hasFocus)
                {
                    g.FillRectangle(Brushes.DarkKhaki, menuItemRectangle);
                    g.DrawString(text, font, Brushes.Black, (RectangleF)menuItemRectangle, labelFormat);
                }

                if (Paint != null)
                {
                    Paint(this, e);
                }
            }

        }

        public void OnGotFocus(EventArgs e)
        {
            //parentMenu.CurrentMenuItem = this;
            hasFocus = true;
            if (GotFocus != null)
            {
                GotFocus(this, e);
            }
            owner.Invalidate();
        }

        public void OnLostFocus(EventArgs e)
        {
            hasFocus = false;
            if (LostFocus != null)
            {
                LostFocus(this, e);
            }
            owner.Invalidate();
        }

        public void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
        {
            foreach (MenuItem m in parentMenu.MenuItemList)
            {
                m.HasFocus = false;
            }
            parentMenu.MenuSwap();

            if (Click != null)
            {
                Click(this, e);
            }
            /*
            parentMenu.CurrentMenuItem = this; // sdavis31 - updating the current Menu Item
            foreach (MenuItem mi in parentMenu.MenuItemList)
            {
                // sdavis31 - hiding the items to close the menu
                mi.Hide();
            }
            if (Click != null)
            {
                Click(this, e);
            }
            parentMenu.Open = false;
             * */
        }

        public void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            if (KeyPress != null)
            {
                KeyPress(this, e);
            }
        }

        public void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                foreach (MenuItem m in parentMenu.MenuItemList)
                {
                    m.HasFocus = false;
                }
                parentMenu.MenuSwap();
                if (Click != null)
                {
                    Click(this, e);
                }
            }
            /*
            try
            {
                // sdavis31 - handling a down click by going to next Menu Item
                if (e.KeyData.ToString() == "Down")
                {
                    if (lastOption == false) // sdavis31 - so not the last item - go to next item
                    {
                        MenuItem forwardItem = parentMenu.MenuItemList[optionNumber];
                        if (forwardItem != null)
                        {
                            owner.FocusHolder = forwardItem;
                            forwardItem.OnGotFocus(null);
                            parentMenu.CurrentMenuItem = forwardItem;
                        }
                    }
                    else // sdavis31 - last item - so go to the first item
                    {
                        MenuItem forwardItem = parentMenu.MenuItemList[0];
                        if (forwardItem != null)
                        {
                            owner.FocusHolder = forwardItem;
                            forwardItem.OnGotFocus(null);
                            parentMenu.CurrentMenuItem = forwardItem;
                        }
                    }
                }

                // sdavis31 - handling an up click by going up to the previous menu item
                if (e.KeyData.ToString() == "Up")
                {
                    if (firstOption == false) // sdavis31 - not first, so go to previous item
                    {
                        MenuItem backItem = parentMenu.MenuItemList[optionNumber - 2];
                        if (backItem != null)
                        {
                            owner.FocusHolder = backItem;
                            backItem.OnGotFocus(null);
                            parentMenu.CurrentMenuItem = backItem;
                        }
                    }
                    else // sdavis31 - first, so go to the last item
                    {
                        MenuItem backItem = parentMenu.MenuItemList[parentMenu.MenuItemList.Count - 1];
                        if (backItem != null)
                        {
                            owner.FocusHolder = backItem;
                            backItem.OnGotFocus(null);
                            parentMenu.CurrentMenuItem = backItem;
                        }
                    }
                }
            }
            catch
            {
            }

            if (KeyDown != null)
            {
                KeyDown(this, e);
            }
             * */
        }

        private void Draw3DBorder(Graphics g)
        {
            int PenWidth = (int)Pens.White.Width;
            
            // sdavis31 - only draw top line if it is the first menu item
            if (this.firstOption == true)
            {
                g.DrawLine(Pens.PaleGoldenrod, new Point(menuItemRectangle.Left, menuItemRectangle.Top),
                    new Point(menuItemRectangle.Right - PenWidth, menuItemRectangle.Top));
            }
            // sdavis31 - left line
            g.DrawLine(Pens.PaleGoldenrod, new Point(menuItemRectangle.Left, menuItemRectangle.Top),
                new Point(menuItemRectangle.Left, menuItemRectangle.Bottom));
            // sdavis31 - only draw the bottom line if it is the last menu item
            if (this.lastOption == true)
            {
                g.DrawLine(Pens.DarkGoldenrod, new Point(menuItemRectangle.Left, menuItemRectangle.Bottom),
                    new Point(menuItemRectangle.Right - PenWidth, menuItemRectangle.Bottom));
            }
            // sdavis31 - right line
            g.DrawLine(Pens.DarkGoldenrod, new Point(menuItemRectangle.Right - PenWidth, menuItemRectangle.Bottom),
                new Point(menuItemRectangle.Right - PenWidth, menuItemRectangle.Top));
        }

        private void DrawFocus(Graphics g)
        {
            int PenWidth = (int)Pens.White.Width;

            g.FillRectangle(Brushes.DarkKhaki, menuItemRectangle.X + PenWidth, menuItemRectangle.Y, 
                menuItemRectangle.Width - PenWidth, menuItemRectangle.Height);

            g.DrawLine(Pens.DarkGoldenrod, new Point(menuItemRectangle.Left, menuItemRectangle.Top),
                new Point(menuItemRectangle.Right - PenWidth, menuItemRectangle.Top));
            g.DrawLine(Pens.DarkGoldenrod, new Point(menuItemRectangle.Left, menuItemRectangle.Top),
                new Point(menuItemRectangle.Left, menuItemRectangle.Bottom));
            g.DrawLine(Pens.PaleGoldenrod, new Point(menuItemRectangle.Left, menuItemRectangle.Bottom),
                new Point(menuItemRectangle.Right - PenWidth, menuItemRectangle.Bottom));
            g.DrawLine(Pens.PaleGoldenrod, new Point(menuItemRectangle.Right - PenWidth, menuItemRectangle.Bottom),
                new Point(menuItemRectangle.Right - PenWidth, menuItemRectangle.Top));

            g.DrawString(text, font, Brushes.Black, (RectangleF)menuItemRectangle, labelFormat);
        }
    }
}
